<template>
  <div class="app-main-container">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item
        :to="{ path: '/application/applicationCenter' }"
        :replace="true"
      >{{ $t('message.yyzx') }}</el-breadcrumb-item>
      <el-breadcrumb-item>{{ $t('message.dcgl') }}</el-breadcrumb-item>
    </el-breadcrumb>
    <el-tabs v-model="activeIndex" class="mt20" @tab-click="tabChange">
      <el-tab-pane
        v-if="meta.indexOf('OrderingBtn') > -1"
        :label="$t('message.dcgl')"
        name="first"
      >
        <div class="warnningTip">
          <i class="el-icon-warning-outline" />
          {{ $t('message.dcgnsypz') }}
        </div>
        <el-tabs v-model="activeTwo" @tab-click="tabClick">
          <el-tab-pane
            v-for="item in ctList"
            :key="item.id"
            :label="item.name"
            :name="item.id"
          />
          <el-table
            ref="table"
            v-loading="listLoading"
            :data="list"
            element-loading-text="Loading"
            border
            fit
            :header-row-class-name="'table-header'"
            class="doumee-element-table"
          >
            <el-table-column align="center" :label="$t('message.cztp')" min-width="120">
              <template slot-scope="scope">
                <el-image
                  shape="circle"
                  style="
                    width: 50px;
                    height: 50px;
                    border-radius: 50%;
                    display: flex;
                    justify-content: center;
                    align-items: center;
                  "
                  fit="cover"
                  :src="scope.row.picture"
                  :preview-src-list="[scope.row.picture]"
                >
                  <div slot="error" class="image-slot">
                    <i class="el-icon-picture" />
                  </div>
                </el-image>
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              :label="$t('message.czmc')"
              min-width="140"
              show-overflow-tooltip
            >
              <template slot-scope="{ row }">
                <el-button type="text" @click="showDetail(row.id)">{{
                  row.name
                }}</el-button>
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              :label="$t('message.czh')"
              min-width="100"
              prop="tableNo"
            />
            <el-table-column
              align="center"
              :label="$t('message.zws')"
              min-width="100"
              prop="seatNum"
            />
            <el-table-column align="center" :label="$t('message.yczt')" min-width="120">
              <template slot-scope="{ row }">
                {{ useStatusToStr(row.useStatus) }}
              </template>
            </el-table-column>
            <el-table-column align="center" :label="$t('message.zfzt')" min-width="120">
              <template slot-scope="{ row }">
                {{ payStatusToStr(row.payStatus) }}
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              :label="$t('message.caozuo')"
              :width="$i18n.locale === 'ZH'? 210 : 450"
              fixed="right"
            >
              <template slot-scope="{ row }">
                <el-button
                  v-if="
                    row.useStatus != 0 &&
                      row.useStatus != 1 &&
                      meta.indexOf('OrderingHZBtn') > -1
                  "
                  v-preventReClick
                  type="text"
                  @click="exchange(row)"
                >{{ $t('message.huanzhuo') }}</el-button>
                <el-button
                  v-if="
                    row.payStatus == 0 &&
                      (row.useStatus == 2 || row.useStatus == 3) &&
                      meta.indexOf('OrderingZFBtn') > -1
                  "
                  v-preventReClick
                  type="text"
                  @click="payAction(row.id, row.useStatus)"
                >{{ $t('message.zf') }}</el-button>
                <el-button
                  v-if="
                    row.useStatus == 3 && meta.indexOf('OrderingJCBtn') > -1
                  "
                  v-preventReClick
                  type="text"
                  @click="oerder(row.id)"
                >{{ $t('message.jc') }}</el-button>
                <el-button
                  v-if="
                    row.useStatus != 0 && meta.indexOf('OrderingHFBtn') > -1
                  "
                  v-preventReClick
                  type="text"
                  @click="recover(row.id)"
                >{{ $t('message.hf') }}</el-button>
                <el-button
                  v-if="
                    row.useStatus == 0 && meta.indexOf('OrderingDCBtn') > -1
                  "
                  v-preventReClick
                  type="text"
                  @click="oerder(row.id)"
                >{{ $t('message.dc') }}</el-button>
                <el-button
                  v-if="
                    row.useStatus == 0 && meta.indexOf('OrderingYYBtn') > -1
                  "
                  v-preventReClick
                  type="text"
                  @click="appointment(row)"
                >{{ $t('message.yuyue') }}</el-button>
              </template>
            </el-table-column>
          </el-table>
          <Pagination
            :total="totalCount"
            :page="pagination.page"
            @current-change="handleCurrentChange"
            @SizeChange="handleSizeChange"
          />
        </el-tabs>
      </el-tab-pane>
      <el-tab-pane
        v-if="meta.indexOf('OrderingOrderBtn') > -1"
        :label="$t('message.dcdd2')"
        name="two"
      >
        <div class="doumee-filter">
          <el-form inline @submit.native.prevent>
            <el-form-item :label="$t('message.ddbh')">
              <el-input
                v-model="orderForm.orderId"
                :placeholder="$t('message.qsr') + $t('message.ddbh')"
                clearable
              />
            </el-form-item>
            <el-form-item :label="$t('message.cpmc')">
              <el-input
                v-model="orderForm.goodsName"
                :placeholder="$t('message.qsr') + $t('message.cpmc')"
                clearable
              />
            </el-form-item>
            <el-form-item :label="$t('message.zffs')">
              <el-select
                v-model="orderForm.payMethod"
                :placeholder="$t('message.qxz') + $t('message.zffs')"
                clearable
              >
                <el-option
                  v-for="item in payTypes"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item :label="$t('message.ddzt')">
              <el-select
                v-model="orderForm.payStatus"
                :placeholder="$t('message.qxz') + $t('message.ddzt')"
                clearable
              >
                <el-option
                  v-for="item in goodsStatus"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item :label="$t('message.ddly')">
              <el-select
                v-model="orderForm.channelType"
                :placeholder="$t('message.qxz') + $t('message.ddly')"
                clearable
              >
                <el-option
                  v-for="item in channelTypeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item :label="$t('message.ct')" prop="goodsRestaurantId">
              <el-select
                v-model="orderForm.goodsRestaurantId"
                :placeholder="$t('message.qxz') + $t('message.ct')"
                clearable
              >
                <el-option
                  v-for="item in ctListb"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-date-picker
                v-model="orderForm.times"
                value-format="yyyy-MM-dd"
                type="daterange"
                :range-separator="$t('message.zhi')"
                clearable
                :start-placeholder="$t('message.ksrq')"
                :end-placeholder="$t('message.jsrq')"
                :picker-options="pickerOptions"
                class="w400"
              />
            </el-form-item>
            <el-form-item>
              <el-button
                v-preventReClick
                type="primary"
                @click="getList(1)"
              >{{ $t('message.cz') }}</el-button>
              <el-button v-preventReClick @click="clearOrder">{{ $t('message.clear') }}</el-button>
              <el-button v-if="meta.indexOf('OrderingOrderDCBtn') > -1" v-preventReClick type="primary" @click="orderExport">{{ $t('message.daochu') }}</el-button>
            </el-form-item>
          </el-form>
        </div>
        <el-table
          ref="tablec"
          v-loading="listLoadingc"
          :data="listc"
          element-loading-text="Loading"
          border
          fit
          :header-row-class-name="'table-header'"
          class="doumee-element-table doumee-element-tableb"
        >
          <el-table-column
            align="center"
            :label="$t('message.ddbh')"
            min-width="210"
            show-overflow-tooltip
          >
            <template v-slot="scope">
              <span
                class="primaryColor pointer"
                @click="handleDetail(scope.row)"
              >{{ scope.row.orderId }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            :label="$t('message.cpxx2')"
            prop="goodsInfo"
            min-width="300"
          >
            <template v-slot="scope">
              <el-tooltip placement="top">
                <div slot="content">
                  <div
                    v-for="(item, index) in scope.row.goodsInfos"
                    :key="index"
                    class="overhiddenText"
                  >
                    {{ item }}
                  </div>
                </div>
                <div class="pointer">
                  <div
                    v-for="(item, index) in scope.row.goodsInfos.slice(0, 2)"
                    :key="index"
                    class="overhiddenText"
                  >
                    {{ item }}
                  </div>
                </div>
              </el-tooltip>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            :label="$t('message.cpzj')"
            prop="allPrice"
            min-width="110"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            :label="$t('message.zffs')"
            prop="payMethod"
            min-width="120"
          >
            <template slot-scope="{ row }">
              {{
                row.payStatus == 0 || row.payStatus == 1 || row.payStatus == 2
                  ? ""
                  : row.payMethod
              }}
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            :label="$t('message.sfk')"
            prop="realPrice"
            min-width="110"
          >
            <template slot-scope="{ row }">
              {{
                row.payStatus == 0 || row.payStatus == 1 || row.payStatus == 2
                  ? ""
                  : row.realPrice
              }}
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            :label="$t('message.ct')"
            prop="restaurantName"
            min-width="120"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            :label="$t('message.canzhuo')"
            prop="tableName"
            min-width="120"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            :label="$t('message.ddly')"
            prop="channelType"
            min-width="120"
          >
            <template v-slot="scope">
              <div>{{ scope.row.channelTypeName }}</div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            :label="$t('message.createTime')"
            prop="createTime"
            show-overflow-tooltip
            min-width="160"
          />
          <el-table-column
            align="center"
            :label="$t('message.paytime')"
            show-overflow-tooltip
            prop="payTime"
            min-width="160"
          />
          <el-table-column
            align="center"
            :label="$t('message.czr')"
            show-overflow-tooltip
            prop="creatorName"
            min-width="100"
          />
          <el-table-column
            align="center"
            :label="$t('message.ddzt')"
            fixed="right"
            min-width="90"
          >
            <template slot-scope="{ row }">
              <span
                v-if="row.payStatus == 0"
                class="primaryColor pointer"
                @click="getOrderType(row.orderId)"
              >{{ row.statusName }}</span>
              <span v-else>{{ row.statusName }}</span>
            </template></el-table-column>
          <el-table-column
            :label="$t('message.bz')"
            show-overflow-tooltip
            prop="note"
            min-width="120"
            align="center"
          />
          <el-table-column
            align="center"
            :label="$t('message.caozuo')"
            :width="$i18n.locale === 'ZH'? 160 : 230"
            fixed="right"
          >
            <template slot-scope="scope">
              <div class="flex-cc">
                <el-button
                  v-if="
                    scope.row.payStatus == 3 &&
                      meta.indexOf('OrderingOrderDYBtn') > -1
                  "
                  v-preventReClick
                  type="text"
                  @click="print(scope.row.orderId, scope.row.saleType)"
                >{{ $t('message.dyxp') }}</el-button>
                <el-button
                  v-if="
                    scope.row.payStatus == 3 &&
                      scope.row.saleType == 1 &&
                      meta.indexOf('OrderingOrderTKBtn') > -1
                  "
                  v-preventReClick
                  type="text"
                  @click="refound(scope.row.orderId)"
                >{{ $t('message.qetk') }}</el-button>
                <el-button
                  v-if="
                    scope.row.payStatus == 0 &&
                      scope.row.channelType == '02' &&
                      meta.indexOf('OrderingOrderZFBtn') > -1
                  "
                  v-preventReClick
                  type="text"
                  @click="continuePay(scope.row)"
                >{{ $t('message.zf') }}</el-button>
                <el-button
                  v-if="
                    scope.row.payStatus == 0 &&
                      meta.indexOf('OrderingOrderQXBtn') > -1
                  "
                  v-preventReClick
                  type="text"
                  class="secondColor"
                  @click="cancleOrder(scope.row.orderId)"
                >{{ $t('message.qx') }}</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <Pagination
          :total="totalCountc"
          :page="paginationc.page"
          @current-change="getList"
          @SizeChange="handleSizeChangec"
        />
      </el-tab-pane>
      <el-tab-pane
        v-if="meta.indexOf('OrderingRestaurantBtn') > -1"
        :label="$t('message.ctsz')"
        name="three"
      >
        <div class="doumee-filter">
          <el-form inline @submit.native.prevent>
            <el-form-item :label="$t('message.ctmc')">
              <el-input
                v-model="filters.keyword"
                :placeholder="$t('message.qsr') + $t('message.ctmc')"
                clearable
              />
            </el-form-item>
            <el-form-item>
              <el-button
                v-preventReClick
                type="primary"
                @click="handleCtList(1)"
              >{{ $t('message.cz') }}</el-button>
              <el-button v-preventReClick @click="clear">{{ $t('message.clear') }}</el-button>
            </el-form-item>
          </el-form>
        </div>
        <div class="doumee_table_header">
          <el-button
            v-if="meta.indexOf('OrderingRestaurantAddBtn') > -1"
            type="primary"
            @click="addCt"
          >{{ $t('message.xinjian') }}</el-button>
        </div>
        <el-table
          ref="tableb"
          v-loading="listLoadingb"
          :data="listb"
          element-loading-text="Loading"
          border
          fit
          :header-row-class-name="'table-header'"
          class="doumee-element-table"
        >
          <el-table-column
            align="center"
            :label="$t('message.ctmc')"
            min-width="140"
            prop="name"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            :label="$t('message.yysj')"
            min-width="120"
            prop="tableNo"
            show-overflow-tooltip
          >
            <template slot-scope="{ row }">
              {{ row.startTime }}~{{ row.endTime }}
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            :label="$t('message.ctwz')"
            min-width="140"
            prop="position"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            :label="$t('message.ddxpj')"
            min-width="140"
            prop="orderPrinterName"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            :label="$t('message.cpxpj')"
            min-width="140"
            prop="mealPrinterName"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            :label="$t('message.cpcpfs')"
            min-width="140"
            prop="mealPrinterType"
            show-overflow-tooltip
          >
            <template slot-scope="{ row }">
              {{ printerType(row.mealPrinterType) }}
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            :label="$t('message.ctyyzt')"
            prop="status"
            min-width="100"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                :active-value="'1'"
                :inactive-value="'0'"
                :disabled="meta.indexOf('OrderingRestaurantYYBtn') == -1"
                @change="upDateStatus(scope.row)"
              />
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            :label="$t('message.yyry')"
            min-width="140"
            prop="operationalStaffName"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            :label="$t('message.createTime')"
            min-width="160"
            prop="createTime"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            :label="$t('message.caozuo')"
            :width="$i18n.locale === 'ZH'? 280 : 440"
            fixed="right"
          >
            <template slot-scope="scope">
              <div class="flex-cc">
                <el-button
                  v-if="meta.indexOf('OrderingRestaurantCPBtn') > -1"
                  type="text"
                  @click="
                    $router.push({
                      name: 'VarietySet',
                      query: { id: scope.row.id },
                    })
                  "
                >{{ $t('message.cpsz2') }}</el-button>
                <el-button
                  v-if="meta.indexOf('OrderingRestaurantCZBtn') > -1"
                  type="text"
                  @click="
                    $router.push({
                      name: 'DinnerTable',
                      query: { id: scope.row.id },
                    })
                  "
                >{{ $t('message.czpz') }}</el-button>
                <el-button
                  v-if="meta.indexOf('OrderingRestaurantEditBtn') > -1"
                  v-preventReClick
                  type="text"
                  @click="toEdit(scope.row)"
                >{{ $t('message.edit') }}</el-button>
                <el-button
                  v-if="meta.indexOf('OrderingRestaurantDelBtn') > -1"
                  v-preventReClick
                  type="text"
                  class="secondColor"
                  @click="deleteBtn(scope.row.id)"
                >{{ $t('message.del') }}</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <Pagination
          :total="totalCountb"
          :page="paginationb.page"
          @current-change="handleCtList"
          @SizeChange="handleSizeChangeb"
        />
      </el-tab-pane>
      <el-tab-pane v-if="meta.indexOf('EatGoodsStatistics') > -1" :label="$t('message.dcxstj')" name="four"><OrderingSales ref="orderingSalesRef" /></el-tab-pane>
      <el-tab-pane v-if="meta.indexOf('EatGoodOrderDetials') > -1" :label="$t('message.dcxsmx')" name="five"><OrderingDetail ref="OrderingDetailsRef" /></el-tab-pane>
    </el-tabs>
    <el-dialog
      :title="$t('message.yuding')"
      :visible.sync="showAppointment"
      :close-on-click-modal="modalDialog"
      width="600px"
      :close-on-press-escape="modalDialog"
    >
      <el-form
        ref="appointmentForm"
        :model="appointmentForm"
        :rules="rules"
        label-suffix="："
        label-width="auto"
        @submit.native.prevent
      >
        <el-form-item :label="$t('message.canzhuo')" prop="goodsTableName">
          {{ appointmentForm.goodsTableName }}
        </el-form-item>
        <el-form-item :label="$t('message.khxm')" prop="userName">
          <el-input
            v-model="appointmentForm.userName"
            :placeholder="$t('message.qsr') + $t('message.khxm')"
          />
        </el-form-item>
        <el-form-item :label="$t('message.khdh')" prop="userPhone">
          <el-input
            v-model="appointmentForm.userPhone"
            :placeholder="$t('message.qsr') + $t('message.khdh')"
          />
        </el-form-item>
        <el-form-item :label="$t('message.ycsj')" prop="makeTime">
          <el-date-picker
            v-model="appointmentForm.makeTime"
            type="datetime"
            style="width: 100%"
            format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss"
            :placeholder="$t('message.qxz') + $t('message.ycsj')"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelAppointment">{{ $t('message.qx') }}</el-button>
        <el-button
          v-preventReClick
          type="primary"
          @click="confirmAppointment"
        >{{ $t('message.qr') }}</el-button>
      </div>
    </el-dialog>
    <el-dialog
      :title="$t('message.huanzhuo')"
      :close-on-click-modal="modalDialog"
      :close-on-press-escape="modalDialog"
      width="600px"
      :visible.sync="showExchange"
    >
      <el-form
        ref="exchangeForm"
        :model="exchangeForm"
        :rules="exchangeRules"
        label-suffix="："
        label-width="auto"
        @submit.native.prevent
      >
        <el-form-item :label="$t('message.dqcz')" prop="goodsTableName">
          {{ exchangeForm.goodsAreaName }} - {{ exchangeForm.goodsTableName }}
        </el-form-item>
        <el-form-item :label="$t('message.ghcz')" prop="goodsTableId2">
          <el-select
            v-model="exchangeForm.goodsTableId2"
            :placeholder="$t('message.qxz')"
            style="width: 100%"
          >
            <el-option
              v-for="item in canUseTables"
              :key="item.id"
              :label="item.goodsAreaName + ' - ' + item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showExchange = false">{{ $t('message.qx') }}</el-button>
        <el-button
          v-preventReClick
          type="primary"
          @click="confirmExchange"
        >{{ $t('message.qr') }}</el-button>
      </div>
    </el-dialog>
    <el-dialog
      :title="ctForm.id ? $t('message.edit') + $t('message.ct') : $t('message.add') + $t('message.ct')"
      width="750px"
      :close-on-click-modal="modalDialog"
      :close-on-press-escape="modalDialog"
      :visible.sync="showAddCt"
    >
      <el-form
        ref="ctForm"
        :model="ctForm"
        :rules="ctrules"
        label-suffix="："
        label-width="auto"
        @submit.native.prevent
      >
        <el-form-item :label="$t('message.ctmc')" prop="name">
          <el-input
            v-model="ctForm.name"
            :placeholder="$t('message.qsr') + $t('message.ctmc')"
            class="w400"
            maxlength="50"
            clearable
          />
        </el-form-item>
        <el-form-item :label="$t('message.yysj')" prop="time">
          <el-time-picker
            v-model="ctForm.time"
            is-range
            class="w400"
            :range-separator="$t('message.zhi')"
            format="HH:mm"
            value-format="HH:mm"
            :start-placeholder="$t('message.kssj')"
            :end-placeholder="$t('message.jssj')"
            :placeholder="$t('message.xzsjfw')"
          />
        </el-form-item>
        <el-form-item :label="$t('message.ctwz')" prop="position">
          <el-input
            v-model="ctForm.position"
            clearable
            class="w400"
            :placeholder="$t('message.qsr') + $t('message.ctwz')"
            maxlength="50"
          />
        </el-form-item>
        <el-form-item :label="$t('message.yyry2')" prop="operationalStaffIds">
          <el-select
            v-model="ctForm.operationalStaffIds"
            :placeholder="$t('message.qxz') + $t('message.yyry2')"
            multiple
            clearable
            class="w400"
          >
            <el-option
              v-for="item in operationalStaffList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('message.ddxpj')" prop="orderPrinterId">
          <div class="df_ac">
            <div>
              <el-select
                v-model="ctForm.orderPrinterId"
                :placeholder="$t('message.qxz') + $t('message.xpj')"
                clearable
                class="xpj"
              >
                <el-option
                  v-for="item in xpjList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
              <el-input
                v-model="ctForm.orderPrinterNum"
                class="w100"
                :placeholder="$t('message.qsr')"
                oninput="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}"
                maxlength="3"
                clearable
              />
            </div>
            <div>
              <div class="tip-style" style="line-height: 16px">
                {{ $t('message.zfcgxpdycs') }}
              </div>
              <div class="tip-style" style="line-height: 16px">
                {{ $t('message.btmr1') }}
              </div>
            </div>
          </div>
        </el-form-item>
        <el-form-item :label="$t('message.cpcpfs')" prop="mealPrinterType">
          <el-select
            v-model="ctForm.mealPrinterType"
            :placeholder="$t('message.qxz') + $t('message.cpcpfs')"
            class="w400"
            clearable
          >
            <el-option
              v-for="item in mealPrinterTypeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          v-if="ctForm.mealPrinterType"
          :label="$t('message.cpxpj2')"
          prop="mealPrinterId"
        >
          <div class="df_ac">
            <div>
              <el-select
                v-model="ctForm.mealPrinterId"
                :placeholder="$t('message.qxz') + $t('message.xpj')"
                clearable
                class="xpj"
              >
                <el-option
                  v-for="item in xpjList2"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
              <el-input
                v-model="ctForm.mealPrinterNum"
                class="w100"
                clearable
                :placeholder="$t('message.qsr')"
                oninput="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}"
                maxlength="3"
              />
            </div>
            <div>
              <div class="tip-style" style="line-height: 16px">
                {{ $t('message.xpdyfs') }}
              </div>
              <div class="tip-style" style="line-height: 16px">
                {{ $t('message.btmr1') }}
              </div>
            </div>
          </div>
        </el-form-item>
        <el-form-item :label="$t('message.yuexiao')">
          <el-switch
            v-model="ctForm.isOpen"
            active-value="1"
            inactive-value="0"
          />
          <span class="placeholder9 ml20">{{ $t('message.kqzsyx') }}</span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showAddCt = false">{{ $t('message.qx') }}</el-button>
        <el-button
          v-preventReClick
          type="primary"
          :loading="addLoading"
          @click="addCtBtn"
        >{{ $t('message.qr') }}</el-button>
      </div>
    </el-dialog>
    <orderDialog ref="child" @orderSuccess="tabChange" />
    <Detail ref="DetailRef" />
    <GoodsTem v-if="isPrint" ref="GoodsTemRef" />
  </div>
</template>

<script>
import {
  goodsTableList, makeAnAppointmen, goodsTableRecover, goodsTableExchange, goodsAreaList,
  goodsCartList,
  goodsRestaurantList2,
  goodsRestaurantList,
  goodsRestaurantSave,
  goodsRestaurantUpdateStatus,
  goodsRestaurantDel,
  goodsRestaurantUpdate,
  printerList,
  goodsOrderList,
  configList,
  orderType,
  goodsOrderCancelPay, refoundOrder, printOrder,
  goodsRestaurantUserList,
  GoodsOrderExport,
  getOrderTypeQuery,
  getChannelType
} from '@/api'
import { TJshortcuts } from '@/utils/config'
import store from '@/store'
import orderDialog from '@/components/order'
import Pagination from '@/components/paginations'
import Detail from './components/detail'
import OrderingDetail from './components/orderingDetail'
import GoodsTem from '@/components/printTem/goods.vue'
import OrderingSales from './components/orderingSales'
export default {
  name: 'Ordering',
  components: { orderDialog, Pagination, Detail, OrderingDetail, OrderingSales, GoodsTem },
  data() {
    return {
      modalDialog: store.getters.dialog,
      meta: [],
      isPrint: false,
      showAddCt: false,
      addLoading: false,
      activeIndex: 'first',
      activeTwo: '',
      ctList: [],
      ctListb: [],
      listLoading: false,
      listLoadingb: true,
      listLoadingc: true,
      filters: {},
      list: [],
      listb: [],
      listc: [],
      goodsArea: [],
      pagination: {
        page: 1,
        rows: 10
      },
      paginationb: {
        page: 1,
        rows: 10
      },
      paginationc: {
        page: 1,
        rows: 10
      },
      totalCount: 0,
      totalCountb: 0,
      totalCountc: 0,
      xpjList: [],
      xpjList2: [],
      mealPrinterTypeList: [{
        label: this.$t('message.xdcp'),
        value: '0'
      }, {
        label: this.$t('message.jscp'),
        value: '1'
      }],
      channelTypeList: [{
        label: this.$t('message.xcx'),
        value: '01'
      }, {
        label: this.$t('message.cgpcd'),
        value: '02'
      }],
      operationalStaffList: [],
      payTypes: [],
      orderForm: {},
      goodsStatus: [
        { value: '0', label: this.$t('message.wzf') },
        { value: '3', label: this.$t('message.yfk') },
        { value: '5', label: this.$t('message.tkcg') }
      ],
      configList: [],
      showAppointment: false,
      appointmentForm: {},
      ctrules: {
        name: [
          {
            required: true, message: this.$t('message.qsr') + this.$t('message.ctmc'), trigger: 'blur'
          }
        ],
        mealPrinterId: [
          { required: true, message: this.$t('message.qxz') + this.$t('message.cpxpj2'), trigger: 'change' }
        ]
      },
      rules: {
        makeTime: [
          { required: true, message: this.$t('message.qsr') + this.$t('message.ycsj'), trigger: 'change' }
        ]
      },
      showExchange: false,
      exchangeForm: {},
      exchangeRules: {
        goodsTableId2: [
          { required: true, message: this.$t('message.qxz') + this.$t('message.cz'), trigger: 'change' }
        ]
      },
      canUseTables: [],
      ctForm: { operationalStaffIds: [], time: [] },
      pickerOptions: {
        shortcuts: TJshortcuts
      }
    }
  },
  created() {
    this.meta = this.$route.meta.buttons || []
    const meta = this.$route.meta.buttons || []
    this.getDefalutConfig()
    if (meta.indexOf('OrderingBtn') > -1) {
      this.toGoodsRestaurantList2()
    } else if (meta.indexOf('OrderingOrderBtn') > -1) {
      this.activeIndex = 'two'
      goodsRestaurantList2({ param: { venueId: sessionStorage.getItem('venueId') }})
        .then(res => {
          if (res.errorCode === '000000') {
            this.ctListb = res.recordList || []
          }
        })
        .catch(() => { })
      this.getList(1)
    } else if (meta.indexOf('OrderingRestaurantBtn') > -1) {
      this.activeIndex = 'three'
      this.handleCtList(1)
    } else if (meta.indexOf('EatGoodsStatistics') > -1) {
      this.activeIndex = 'four'
      this.$refs.orderingSalesRef.getLoading()
    } else if (meta.indexOf('EatGoodOrderDetials') > -1) {
      this.activeIndex = 'five'
      this.$refs.OrderingDetailsRef.getLoading()
    }
    if (this.$route.query.activeTwo) {
      this.activeTwo = this.$route.query.activeTwo
      this.handleCurrentChange(1)
    }
  },
  activated() {
    console.log('actived')
    if (this.$route.query.activeTwo) {
      this.activeTwo = this.$route.query.activeTwo
      this.handleCurrentChange(1)
    }
  },
  methods: {
    handleSizeChangeb(value) {
      this.paginationb.rows = value
      this.handleCtList(1)
    },
    handleSizeChangec(value) {
      this.paginationc.rows = value
      this.getList(1)
    },
    getDefalutConfig() {
      configList({ param: { type: '2', code: '1' }})
        .then((res) => {
          if (res.errorCode !== '000000') return
          this.payTypes = res.recordList
        })
        .catch((err) => {
          console.log(err)
        })
    },
    tomanufacturerList() {
      printerList({ param: { useType: 1, venueId: sessionStorage.getItem('venueId') }})
        .then(res => {
          if (res.errorCode === '000000') {
            this.xpjList = res.recordList
          }
        })
      printerList({ param: { useType: 0, venueId: sessionStorage.getItem('venueId') }})
        .then(res => {
          if (res.errorCode === '000000') {
            this.xpjList2 = res.recordList
          }
        })
    },
    togoodsRestaurantUserList() {
      goodsRestaurantUserList({ param: { venueId: sessionStorage.getItem('venueId') }})
        .then(res => {
          if (res.errorCode === '000000') {
            this.operationalStaffList = res.recordList
          }
        })
    },
    tabChange() {
      if (this.activeIndex === 'three') {
        this.handleCtList(1)
      } else if (this.activeIndex === 'two') {
        this.getList(1)
        this.getChannel()
        this.getOrderStatusList()
      } else if (this.activeIndex === 'first') {
        this.handleCurrentChange(1)
      } else if (this.activeIndex === 'four') {
        this.$refs.orderingSalesRef.getLoading()
      } else {
        this.$refs.OrderingDetailsRef.getLoading()
        this.$refs.OrderingDetailsRef.getChannel()
      }
    },
    getChannel() {
      getChannelType({ param: { code: 'CHANNEL_GOODS' }}).then(res => {
        this.channelTypeList = res.recordList.map(item => {
          return {
            label: item.name,
            value: item.value
          }
        })
      })
    },
    getOrderStatusList() {
      getOrderTypeQuery({ param: {}}).then(res => {
        this.goodsStatus = res.recordList.map(item => {
          return {
            label: item.name,
            value: item.value
          }
        })
      })
    },
    handleDetail(item) {
      this.$refs.DetailRef.getOrderDetail(item.orderId)
      this.$refs.DetailRef.isShow = true
    },
    tabClick() {
      this.handleCurrentChange(1)
    },
    addCt() {
      this.tomanufacturerList()
      this.togoodsRestaurantUserList()
      this.showAddCt = true
      this.ctForm = { operationalStaffIds: [], time: [], isOpen: '1' }
      const venueInfo = JSON.parse(sessionStorage.getItem('venueInfo'))
      if (venueInfo && venueInfo.openTimeStart && venueInfo.openTimeEnd) {
        this.$set(this.ctForm, 'time', [
          venueInfo.openTimeStart,
          venueInfo.openTimeEnd
        ])
      }
    },
    toEdit(row) {
      this.tomanufacturerList()
      this.togoodsRestaurantUserList()
      this.showAddCt = true
      this.ctForm = { ...row }
      if (row.startTime && row.endTime) {
        this.$set(this.ctForm, 'time', [row.startTime, row.endTime])
      } else {
        this.$set(this.ctForm, 'time', [])
        const venueInfo = JSON.parse(sessionStorage.getItem('venueInfo'))
        if (venueInfo && venueInfo.openTimeStart && venueInfo.openTimeEnd) {
          this.$set(this.ctForm, 'time', [
            venueInfo.openTimeStart,
            venueInfo.openTimeEnd
          ])
        }
      }
      console.log(312, this.ctForm)
    },
    addCtBtn() {
      const param = { ...this.ctForm }
      if (param.time) {
        param.startTime = param.time[0]
        param.endTime = param.time[1]
      } else {
        param.startTime = ''
        param.endTime = ''
      }
      if (!param.mealPrinterNum && param.mealPrinterId) {
        param.mealPrinterNum = 1
      }
      if (!param.orderPrinterNum && param.orderPrinterId) {
        param.orderPrinterNum = 1
      }
      param.venueId = sessionStorage.getItem('venueId')
      this.$refs.ctForm.validate((v) => {
        if (v) {
          this.addLoading = true
          if (param.id) {
            goodsRestaurantUpdate({ param })
              .then(res => {
                this.addLoading = false
                if (res.errorCode === '000000') {
                  this.$message.success(this.$t('message.editcg'))
                  this.$refs.ctForm.resetFields()
                  this.showAddCt = false
                  this.handleCtList(1)
                  this.toGoodsRestaurantList2()
                }
              }, () => {
                this.addLoading = false
              })
          } else {
            param.status = '0'
            goodsRestaurantSave({ param })
              .then(res => {
                this.addLoading = false
                if (res.errorCode === '000000') {
                  this.$message.success(this.$t('message.addcg'))
                  this.$refs.ctForm.resetFields()
                  this.showAddCt = false
                  this.handleCtList(1)
                  this.toGoodsRestaurantList2()
                }
              }, () => {
                this.addLoading = false
              })
          }
        }
      })
    },
    upDateStatus(row) {
      // 更新状态
      const arr = this.listb
      const obj = { ...row }
      this.listLoadingb = true
      goodsRestaurantUpdateStatus({
        param: {
          id: obj.id,
          status: obj.status
        }
      })
        .then((res) => {
          this.listLoadingb = false
          if (res.errorCode === '000000') {
            if (obj.status === '1') {
              this.$message.success(this.$t('message.qycg'))
            } else {
              this.$message.success(this.$t('message.jycg'))
            }
            this.handleCtList(1)
            this.toGoodsRestaurantList2()
          } else {
            arr.forEach((e) => {
              if (e.id === obj.id) {
                e.status = row.status === '1' ? '0' : '1'
              }
            })
            this.listb = arr
          }
        })
        .catch(() => {
          this.listLoadingb = false
          arr.forEach((e) => {
            if (e.id === obj.id) {
              e.status = row.status === '1' ? '0' : '1'
            }
          })
          this.listb = arr
        })
    },
    deleteBtn(id) {
      const that = this
      this.$confirm(this.$t('message.sccttc'), this.$t('message.ts'), {
        confirmButtonText: this.$t('message.qd'),
        cancelButtonText: this.$t('message.qx'),
        type: 'warning',
        callback: () => {},
        beforeClose: (action, ctx, close) => {
          if (action !== 'confirm') {
            close()
            return
          }
          ctx.confirmButtonLoading = true
          goodsRestaurantDel({ param: { id }}).then((res) => {
            ctx.confirmButtonLoading = false
            close()
            if (res.errorCode === '000000') {
              that.$message.success(this.$t('message.delcg'))
              that.handleCtList(1)
              that.toGoodsRestaurantList2()
            }
          })
            .catch(() => { ctx.confirmButtonLoading = false })
        }
      }).finally(() => {})
    },
    handleCtList(page) {
      if (page) {
        this.paginationb.page = page
      }
      this.listLoadingb = true
      goodsRestaurantList({
        pagination: { ...this.paginationb },
        param: {
          venueId: sessionStorage.getItem('venueId'),
          ...this.filters
        }
      })
        .then(res => {
          this.listLoadingb = false
          if (res.errorCode === '000000') {
            this.listb = res.recordList
            this.totalCountb = res.totalCount
            this.$refs.tableb.doLayout()
          }
        })
        .catch(() => { this.listLoadingb = false })
    },
    clearOrder() {
      this.orderForm = {}
      this.getList(1)
    },
    getList(page) {
      if (page) {
        this.paginationc.page = page
      }
      this.listLoadingc = true
      const orderForm = { ...this.orderForm }
      orderForm.saleType = '1'
      if (orderForm.times && orderForm.times.length > 0) {
        orderForm.startTimeQ = orderForm.times[0] + ' 00:00:00'
        orderForm.endTimeQ = orderForm.times[1] + ' 23:59:59'
      }
      goodsOrderList({
        pagination: { ...this.paginationc },
        param: {
          venueId: sessionStorage.getItem('venueId'),
          ...orderForm
        }
      })
        .then(res => {
          this.listLoadingc = false
          if (res.errorCode === '000000') {
            this.listc = res.recordList
            this.totalCountc = res.totalCount
            this.listc.forEach(item => {
              item.goodsInfos = item.goodsInfo.split('\n')
            })
            this.$refs.tablec.doLayout()
          }
        })
        .catch(() => { this.listLoadingc = false })
    },
    orderExport() {
      const orderForm = { ...this.orderForm }
      orderForm.saleType = '1'
      if (orderForm.times && orderForm.times.length > 0) {
        orderForm.startTimeQ = orderForm.times[0] + ' 00:00:00'
        orderForm.endTimeQ = orderForm.times[1] + ' 23:59:59'
      }
      GoodsOrderExport({
        param: {
          venueId: sessionStorage.getItem('venueId'),
          ...orderForm
        }
      })
        .then(res => {
          if (res.errorCode === '000000') {
            const elink = document.createElement('a')
            elink.style.display = 'none'
            elink.href = res.record.showUrl
            document.body.appendChild(elink)
            elink.click()
            URL.revokeObjectURL(elink.href)
            document.body.removeChild(elink)
          }
        })
        .catch(() => { })
    },
    toGoodsRestaurantList2() {
      goodsRestaurantList2({ param: { venueId: sessionStorage.getItem('venueId'), status: '1' }})
        .then(res => {
          if (res.errorCode === '000000') {
            if (res.recordList && res.recordList.length > 0) {
              this.ctList = res.recordList
              this.activeTwo = res.recordList[0].id
              this.handleCurrentChange(1)
            } else {
              this.totalCount = 0
              this.list = []
              this.ctList = []
            }
          }
        })
        .catch(() => { })
      goodsRestaurantList2({ param: { venueId: sessionStorage.getItem('venueId') }})
        .then(res => {
          if (res.errorCode === '000000') {
            this.ctListb = res.recordList || []
          }
        })
        .catch(() => { })
    },
    toGoodsAreaList() {
      goodsAreaList({ param: { venueId: sessionStorage.getItem('venueId') }})
        .then(res => {
          if (res.errorCode === '000000') {
            this.goodsArea = res.recordList
          }
        })
        .catch(() => { })
    },
    appointment(item) {
      this.appointmentForm.goodsTableId = item.id
      this.appointmentForm.goodsTableName = item.name
      this.showAppointment = true
    },
    clear() {
      this.filters = {}
      this.handleCtList(1)
    },
    handleSizeChange(value) {
      this.pagination.rows = value
      this.handleCurrentChange(1)
    },
    handleCurrentChange(page) {
      if (page) {
        this.pagination.page = page
      }
      this.listLoading = true
      goodsTableList({
        pagination: { ...this.pagination },
        param: {
          venueId: sessionStorage.getItem('venueId'),
          goodsRestaurantId: this.activeTwo,
          status: '1'
        }
      })
        .then(res => {
          this.listLoading = false
          if (res.errorCode === '000000') {
            this.list = res.recordList
            this.totalCount = res.totalCount
            this.$refs.table.doLayout()
          }
        })
        .catch(() => { this.listLoading = false })
    },
    useStatusToStr(status) {
      switch (status) {
        case '0': return this.$t('message.kx')
        case '1': return this.$t('message.yyy')
        case '2': return this.$t('message.dcz')
        case '3': return this.$t('message.yxd')
        default: return '-'
      }
    },
    printerType(status) {
      switch (status) {
        case '0': return this.$t('message.xdcp')
        case '1': return this.$t('message.jscp')
        default: return '-'
      }
    },
    payStatusToStr(status) {
      switch (status) {
        case '0': return this.$t('message.wzf')
        case '1': return this.$t('message.ypay')
        case '2': return this.$t('message.bfzf')
        default: return '-'
      }
    },
    cancelAppointment() {
      this.$refs.appointmentForm.resetFields()
      this.showAppointment = false
    },
    confirmAppointment() {
      this.$refs.appointmentForm.validate((valid) => {
        if (!valid) {
          return false
        }
        const param = { ...this.appointmentForm, venueId: sessionStorage.getItem('venueId') }
        makeAnAppointmen({ param })
          .then(res => {
            if (res.errorCode === '000000') {
              this.$message.success(this.$t('message.yycg'))
              this.cancelAppointment()
              this.handleCurrentChange(1)
            } else {
              this.$message.error(this.$t('message.yysb'))
            }
          })
          .catch(() => { })
      })
    },
    recover(goodsTableId) {
      goodsTableRecover({
        param: {
          venueId: sessionStorage.getItem('venueId'),
          goodsTableId
        }
      })
        .then(res => {
          if (res.errorCode === '000000') {
            this.$message.success(this.$t('message.hfcg'))
            this.handleCurrentChange(1)
          } else {
            this.$message.error(this.$t('message.hfsb'))
          }
        })
    },
    payAction(id, status) {
      if (status === 2) {
        this.oerder(id)
        return
      }
      goodsCartList({
        param: {
          goodsTableId: id,
          venueId: sessionStorage.getItem('venueId')
        }
      })
        .then(res => {
          if (res.errorCode !== '000000') return
          const cartList = res.record.cartList
          const goodsList = []
          cartList.forEach(item => {
            item.goodsList.forEach(goods => {
              goodsList.push({
                id: goods.goodsId,
                goodsNum: goods.num,
                skuInfo: goods.goodsSkuInfo
              })
            })
          })
          this.$refs.child.continuePay(goodsList, 'table', id)
        })
    },
    oerder(id) {
      this.$router.push({ name: 'OrderDinner', query: { id, goodsRestaurantId: this.activeTwo }})
    },
    exchange(row) {
      this.exchangeForm.goodsTableName = row.name
      this.exchangeForm.goodsAreaName = row.goodsAreaName
      this.exchangeForm.goodsTableId1 = row.id
      this.showExchange = true
      goodsTableList({
        param: {
          venueId: sessionStorage.getItem('venueId'),
          goodsRestaurantId: this.activeTwo,
          status: '1',
          useStatus: '0'
        }
      })
        .then(res => {
          if (res.errorCode === '000000') {
            this.canUseTables = res.recordList
          }
        })
    },
    confirmExchange() {
      this.$refs.exchangeForm.validate((v) => {
        if (v) {
          goodsTableExchange({ param: { ...this.exchangeForm }})
            .then(res => {
              if (res.errorCode === '000000') {
                this.$message.success(this.$t('message.hzcg'))
                this.$refs.exchangeForm.resetFields()
                this.showExchange = false
                this.handleCurrentChange(1)
              }
            })
        }
      })
    },
    showDetail(id) {
      this.$router.push({ name: 'OrderingDetail', query: { id }})
    },
    // 订单操作
    getOrderType(orderId) {
      orderType({
        param: {
          orderId
        }
      })
        .then((res) => {
          const str = res.record.msg
          if (res.errorCode === '000000') {
            if (res.record.code === '0') {
              this.$message.success(str)
              this.getList(1)
            } else if (res.record.code === '2') {
              this.$message.info(str)
            } else {
              this.$message.error(str)
              this.getList(1)
            }
          }
        })
        .catch((err) => {
          console.log(err)
        })
    },
    print(id, saleType) {
      this.$confirm(this.$t('message.dyxptc'), this.$t('message.ts'), {
        confirmButtonText: this.$t('message.qd'),
        cancelButtonText: this.$t('message.qx'),
        type: 'warning'
      })
        .then(() => {
          // this.isPrint = true
          printOrder({
            param: {
              contentMap: {},
              venueId: sessionStorage.getItem('venueId'),
              workId: id,
              templateCode: 'GOODSTPML2'
            }
          })
            .then((res) => {
              // if (res.record && res.record.isPrint === '1' && res.record.dataList && res.record.dataList.length > 0) {
              //   res.record.dataList.forEach(item => {
              //     this.$refs.GoodsTemRef.obj = { ...item }
              //     setTimeout(() => {
              //       this.$refs.GoodsTemRef.print()
              //     }, 300)
              //     setTimeout(() => {
              //       this.isPrint = false
              //     }, 500)
              //   })
              // } else {
              //   this.isPrint = false
              // }
            })
            .catch((err) => {
              this.isPrint = false
              console.log(err)
            })
        })
        .catch(() => {
          // this.$message.info('操作已取消')
        })
    },
    refound(id) {
      const that = this
      this.$confirm(this.$t('message.ddtkct'), this.$t('message.tixing'), {
        confirmButtonText: this.$t('message.qd'),
        cancelButtonText: this.$t('message.qx'),
        type: 'warning',
        callback: () => {},
        beforeClose: (action, ctx, close) => {
          if (action !== 'confirm') {
            close()
            return
          }
          ctx.confirmButtonLoading = true
          refoundOrder({ param: { id }}).then((res) => {
            ctx.confirmButtonLoading = false
            close()
            if (res.errorCode === '000000') {
              that.$message.success(this.$t('message.tkcg'))
              that.getList(1)
            }
          })
            .catch(() => { ctx.confirmButtonLoading = false })
        }
      }).finally(() => {})
    },
    // 继续支付
    continuePay(row) {
      const { orderId } = row
      // goodsCartList({ param: { goodsTableId: tableId, venueId: sessionStorage.getItem('venueId') }}).then(res=>{
      this.$refs.child.continuePay(orderId, 'goods', 'PAY_METHOD_DIANCAN')
      // })
    },
    cancleOrder(id) {
      const that = this
      this.$confirm(this.$t('message.qxddtc'), this.$t('message.ts'), {
        confirmButtonText: this.$t('message.qd'),
        cancelButtonText: this.$t('message.qx'),
        type: 'warning',
        callback: () => {},
        beforeClose: (action, ctx, close) => {
          if (action !== 'confirm') {
            close()
            return
          }
          ctx.confirmButtonLoading = true
          goodsOrderCancelPay({ param: { id }}).then((res) => {
            ctx.confirmButtonLoading = false
            close()
            if (res.errorCode === '000000') {
              that.$message.success(this.$t('message.qxcg'))
              that.getList(1)
            }
          })
            .catch(() => { ctx.confirmButtonLoading = false })
        }
      }).finally(() => {})
    }
  }
}
</script>

<style lang="scss" scoped>
.xpj {
  width: 280px;
  margin-right: 20px;
}
</style>
